package com.study.hospital_3.service;

import com.study.hospital_3.mapper.DrugMapper;
import com.study.hospital_3.pojo.Drug;
import com.study.hospital_3.vo.DrugUpdateVO;
import com.study.hospital_3.vo.DrugVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

    @Service
    public class DrugService {

        @Resource
        private DrugMapper drugMapper;

        public Map<String, Object> selectDrugs(DrugVO drugVO) {
            System.out.println("--------------------------------------------------------------------");
            System.out.println(drugVO.getPage());
            System.out.println(drugVO.getLimit());
            System.out.println(drugVO.getId());
            System.out.println(drugVO.getDrugname());
            System.out.println(drugVO.getDrugid());
            System.out.println("--------------------------------------------------------------------");
            Map<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "查询用户列表成功");
            map.put("data", drugMapper.selectDrugs(drugVO));
            return map;
        }

        public Map<String, Object> insert(DrugVO drugVO) {

            Drug drug = new Drug();
            BeanUtils.copyProperties(drugVO,drug);   // 把前端传递的值对象数据，自动封装到pojo对象中去(反射机制)

            // 手动修改用户密码为md5值
//            String md5 = DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8));
//            user.setPassword(md5);

            //System.out.println(drug.getDrug_name());
           drugMapper.insert(drug);
            Map<String, Object> map = new HashMap<>();
            map.put("code", 200);
            map.put("msg", "新增药品成功");
            map.put("data", drug);
            return map;
        }


        /**
         * 根据指定的主键id删除一个用户记录
         * @param id  用户的主键id
         * @return    返回删除的状态和信息
         */
        public Map<String, Object> deleteById(Integer id) {
            Map<String, Object> map = new HashMap<>();
            drugMapper.deleteById(id);
            map.put("code", 200);
            map.put("msg", "删除药品成功");
            return map;
        }

        public Map<String, Object> updateById(DrugUpdateVO drugUpdateVO) {
            Map<String, Object> map = new HashMap<>();
            drugMapper.updateById(drugUpdateVO);
            map.put("code", 200);
            map.put("msg", "更新药品成功");
            return map;
        }


    }
